package com.sctvcloud.zhaohua_sd.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.jiongbull.jlog.JLog;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 高德定位
 */
public class GaoDeLocationUtil implements AMapLocationListener {

    private static GaoDeLocationUtil instance;
    private Context context;
    private static final String TAG = "GaoDeLocationUtil";
    //声明mlocationClient对象
    public AMapLocationClient mlocationClient;
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;

    private Set<LocationChangeListener> locationChangeListener = new HashSet<LocationChangeListener>();

    public void registerOnLocatedListener(LocationChangeListener l) {
        unregisterOnLocatedListener(l);
        locationChangeListener.add(l);
    }

    public void unregisterOnLocatedListener(LocationChangeListener l) {
        if (locationChangeListener.contains(l)) {
            locationChangeListener.remove(l);
        }
    }

    public static synchronized GaoDeLocationUtil getInstance() {
        if (null == instance) {
            instance = new GaoDeLocationUtil();
        }
        return instance;
    }

    private void init(Context context) {
        if (null == mlocationClient) {
            mlocationClient = new AMapLocationClient(context);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mlocationClient.setLocationListener(this);
            mLocationOption.setOnceLocation(false);
            //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位间隔,单位毫秒,默认为2000ms
            mLocationOption.setInterval(2000);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除

        }
        if (!mlocationClient.isStarted()) {
            //启动定位
            mlocationClient.startLocation();
        }
    }

    /**
     * 启动定位
     *
     * @param context
     */
    public void startLocate(Context context) {
        init(context);
        if (mlocationClient != null && mlocationClient.isStarted()) {
            mlocationClient.startLocation();
        } else {
            JLog.e(TAG + "locClient is null or not started");
        }
    }

    /**
     * 关闭定位
     */
    public void stopLocate() {
        if (null != mlocationClient) {
            JLog.e(TAG + "locClient is stoped");
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
            mlocationClient = null;
        }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (null != locationChangeListener) {
            for (LocationChangeListener l : locationChangeListener) {
                if (null != l) {
                    l.onLocationChange(aMapLocation);
                }
            }

        }

    }
    public interface LocationChangeListener {
        void onLocationChange(AMapLocation location);
    }
}
